package com.koron.css2.workflow;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.PubReceiptBean;
import com.koron.css2.baseConfig.mapper.PubReceiptMapper;
import com.koron.css2.serviceManage.bean.ProcessNodeBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.workflow.bean.ApprovalCommentConfigBean;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 查询流程所有节点信息
 * @author admin
 *
 */
public class ProcessNodeList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {
			Map<String, String> map = JsonUtils.objectToPojo(req.getData(), HashMap.class);
			Object companyNo = map.get("companyNo");
            String type = map.get("type");
			if(companyNo == null) {  
				companyNo = userInfo.getCurWaterCode();
			}
            PubReceiptMapper pubReceiptMapper = factory.getMapper(PubReceiptMapper.class);
            String setCode = pubReceiptMapper.getWorkflowApp(userInfo.getCurWaterCode());
			//读取对应水司的流程平台应用密钥
			PubReceiptMapper mapper = factory.getMapper(PubReceiptMapper.class, DBSourceUtils.getDbEnv(companyNo + ""));
			List<ProcessNodeBean> list = mapper.listProcessNodeByBillType(map);
            if (StringUtils.isNotEmpty(type) && Objects.equals("2", type)){
                for (ProcessNodeBean bean : list){
                    List<ApprovalCommentConfigBean> data = WorkflowImpl.listApprovalCommentConfig(setCode, bean.getReceiptType(), bean.getNodeCode());
                    String comments = data.stream().map(t -> Objects.toString(t.getComment(), "")).collect(Collectors.joining(","));
                    bean.setComment(comments);
                    bean.setSetCode(setCode);
                    PubReceiptBean pubReceipt = pubReceiptMapper.selectPubReceipt(bean.getReceiptType());
                    bean.setReceiptName(pubReceipt.getReceiptName());
                }
            }

			if(StringUtils.isBlank(map.get("nodeCode"))) {
				ProcessNodeBean hadBean = new ProcessNodeBean();
				hadBean.setNodeCode(ProcessStatusEnum.END.getStatus()); 
				hadBean.setNodeName(ProcessStatusEnum.END.getName());
				ProcessNodeBean cancelBean = new ProcessNodeBean();
				//cancelBean.setNodeCode(ProcessStatusEnum.CANCEL.getStatus());
				//cancelBean.setNodeName(ProcessStatusEnum.CANCEL.getName());
				ProcessNodeBean terminationBean = new ProcessNodeBean();
				terminationBean.setNodeCode(ProcessStatusEnum.TERMINATION.getStatus());
				terminationBean.setNodeName(ProcessStatusEnum.TERMINATION.getName());
				list.add(cancelBean);
				list.add(hadBean);
			}

			//去掉起草节点
			Iterator<ProcessNodeBean> ite = list.iterator();
			while(ite.hasNext()) {
				ProcessNodeBean node = ite.next();
				if("起草节点".equals(node.getNodeName())) {
					ite.remove();
				}
			}
			info.setData(list);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}
}
